package web.adminUI;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import web.sharedUI.ErrorMessage;
import web.sharedUI.SessionBean;
import ejb.contentManagement.ExcursionMgr;
import ejb.contentManagement.FlightMgr;
import ejb.contentManagement.HotelMgr;
import ejb.dto.ExcursionDTO;
import ejb.dto.FlightDTO;
import ejb.dto.HotelDTO;
import ejb.dto.PackageDTO;
import ejb.reservationManagement.PackageMgr;


@ManagedBean
@SessionScoped
public class PackageBean {
	
	public PackageBean() {
		pack = new PackageDTO();
		setErrorMessage(null);
	}
	
	@EJB
	private PackageMgr packageMgr; 
	
	@EJB
	private HotelMgr hotelMgr;
	
	@EJB
	private ExcursionMgr excursionMgr;
	
	@EJB
	private FlightMgr flightMgr;
	
	@SuppressWarnings("unused")
	private List<PackageDTO> allPackages;
	private List<ExcursionDTO> availableExcursions;
	private List<HotelDTO> availableHotels;
	private List<FlightDTO> departureFlights;
	private List<FlightDTO> returningFlights;
	private int packageId;
	private PackageDTO pack;
	private String errorMessage;

	/**
	 * Delete Package from database
	 * @param id
	 * @return redirect to page <code>managePackages</code>
	 */
	public String deletePackage(int id) {
		boolean result = packageMgr.deletePackage(id);
		if(!result){
			SessionBean.displayError(ErrorMessage._DELETE);
			return null;
		}
		return "/admins/managePackages?faces-redirect=true";
	}
	
	/**
	 * Update Package in database
	 * @return redirect to page <code>managePackages</code>
	 */
	public String updatePackage() {
		boolean result = packageMgr.updatePackage(pack);
		if(!result){
			SessionBean.displayError(ErrorMessage._UPDATE);
			return null;
		}
		return "/admins/managePackages?faces-redirect=true";
	}
	
	/**
	 * Insert Package in database
	 * @return redirect to page <code>managePackages</code>
	 */
	public String insertPackage() {
		boolean result = packageMgr.insertPackage(pack);
		if(!result){
			SessionBean.displayError(ErrorMessage._INSERT);
			return null;
		}
		return "/admins/managePackages?faces-redirect=true";
	}
	
	/*
	 * GETTERS & SETTERS
	 */
	
	public List<PackageDTO> getAllPackages() {
		return packageMgr.getAllPackagesDTO();
	}

	public void setAllPackages(List<PackageDTO> allPackages) {
		this.allPackages = allPackages;
	}


	public int getPackageId() {
		return packageId;
	}

	public void setPackageId(int packageId) {
		this.packageId = packageId;
		pack = packageMgr.getPackageDTO(packageId);
		if(pack == null) {
			SessionBean.notFound();
		}
	}

	public PackageDTO getPack() {
		return pack;
	}

	public void setReturningFlights(List<FlightDTO> returningFlights) {
		this.returningFlights = returningFlights;
	}
	
	
	public void setPack(PackageDTO pack) {
		this.pack = pack;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public List<ExcursionDTO> getAvailableExcursions() {
		availableExcursions = excursionMgr.getAllExcursionsByLocation(pack.getLocationId());
		return availableExcursions;
	}

	public List<ExcursionDTO> getAdditionalExcursions() {
		List<ExcursionDTO> additionalExcursions = new ArrayList<ExcursionDTO>();
		for(ExcursionDTO e : availableExcursions) {
			if(pack.getExcursionId() != e.getId()) {
				additionalExcursions.add(e);
			}
		}
		return additionalExcursions;	
	}
	
	public List<HotelDTO> getAdditionalHotels() {
		List<HotelDTO> additionalHotels = new ArrayList<HotelDTO>();
		for(HotelDTO h : availableHotels) {
			if(pack.getHotelId() != h.getId()) {
				additionalHotels.add(h);
			}
		}
		return additionalHotels;	
	}
	
	public void setAvailableExcursions(List<ExcursionDTO> availableExcursions) {
		this.availableExcursions = availableExcursions;
	}

	public List<HotelDTO> getAvailableHotels() {
		availableHotels = hotelMgr.getAllHotelsByLocation(pack.getLocationId());
		return availableHotels;
	}
	

	public void setAvailableHotels(List<HotelDTO> availableHotels) {
		this.availableHotels = availableHotels;
	}	
	
	public List<FlightDTO> getDepartureFlights(){
		departureFlights = flightMgr.getAllDepFlightsByLocation(pack.getLocationId(), pack.getPeriodId());
		return departureFlights;
	}
	
	public List<FlightDTO> getReturningFlights(){
		returningFlights = flightMgr.getAllRetFlightsByLocation(pack.getLocationId(), pack.getPeriodId());
		return returningFlights;
	}
	
	public void setDepartureFlights(List<FlightDTO> departureFlights) {
		this.departureFlights = departureFlights;
	}

	
}
